package com.easylive.web.controller;

import com.easylive.web.annotation.GlobalInterceptor;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.enums.ResponseCodeEnum;
import com.easylive.entity.enums.VideoStatusEnum;
import com.easylive.entity.po.*;
import com.easylive.entity.query.VideoDanmuQuery;
import com.easylive.entity.query.VideoInfoFilePostQuery;
import com.easylive.entity.query.VideoInfoPostQuery;
import com.easylive.entity.request.ucenter.WebUcenterLoadVideoList;
import com.easylive.entity.request.ucenter.WebUcenterPostVideoReqVO;
import com.easylive.entity.vo.PaginationResultVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.entity.vo.VideoPostEditInfoVO;
import com.easylive.entity.vo.VideoStatusCountInfoVO;
import com.easylive.exception.BusinessException;
import com.easylive.service.*;
import com.easylive.utils.JsonUtil;
import com.easylive.utils.StringTools;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * @Author: yudaxian
 * @DateTime: 2024/11/8 15:58
 **/
@RestController
@RequestMapping("/ucenter")
public class UcenterViodePostController extends ABaseController {

    /**
     * 发布用的
     */
    @Resource
    private VideoInfoPostService videoInfoPostService;

    @Resource
    private VideoInfoFilePostService videoInfoFilePostService;

    /**
     * 查看用的
     */
    @Resource
    private VideoInfoService videoInfoService;

    @Resource
    private VideoInfoFileService videoInfoFileService;

    @Resource
    private VideoCommentService videoCommentService;

    @Resource
    private VideoDanmuService videoDanmuService;

    /**
     * 发布和修改视频
     * @param reqVO
     * @return
     */
    @PostMapping("/postVideo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO postVideo(@Validated WebUcenterPostVideoReqVO reqVO) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        
        List<VideoInfoFilePost> filePostList = JsonUtil.convertJsonArray2List(reqVO.getUploadFileList(), VideoInfoFilePost.class);

        VideoInfoPost videoInfoPost = new VideoInfoPost();
        videoInfoPost.setVideoId(reqVO.getVideoId());
        videoInfoPost.setVideoCover(reqVO.getVideoCover());
        videoInfoPost.setVideoName(reqVO.getVideoName());
        videoInfoPost.setpCategoryId(reqVO.getPCategoryId());
        videoInfoPost.setCategoryId(reqVO.getCategoryId());
        videoInfoPost.setPostType(reqVO.getPostType());
        videoInfoPost.setTags(reqVO.getTags());
        videoInfoPost.setIntroduction(reqVO.getIntroduction());
        videoInfoPost.setInteraction(reqVO.getInteraction());

        videoInfoPost.setUserId(tokenUserInfoDto.getUserId());

        videoInfoPostService.saveVideoInfoPost(videoInfoPost, filePostList);

        return getSuccessResponseVO(null);
    }

    @PostMapping("/loadVideoList")
    public ResponseVO loadVideoList(WebUcenterLoadVideoList reqVO) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();

        VideoInfoPostQuery query = new VideoInfoPostQuery();
        query.setUserId(tokenUserInfoDto.getUserId());
        query.setPageNo(reqVO.getPageNo());
        query.setOrderBy("v.create_time desc");
        if (!StringTools.isEmpty(reqVO.getVideoNameFuzzy())) {
            query.setVideoNameFuzzy(reqVO.getVideoNameFuzzy());
        }
        if (reqVO.getStatus() != null) {
            /**
             * -1: 前端传过来的，表示进行中，需要排除已通过的和不通过的
             */
            if (reqVO.getStatus() == -1) {
                query.setExcludeStatusArray(new Integer[]{VideoStatusEnum.STATUS3.getStatus(), VideoStatusEnum.STATUS4.getStatus()});
            } else {
                /**
                 * 否则查什么就是什么，前端另外两个状态：已通过和不通过
                 */
                query.setStatus(reqVO.getStatus());
            }
        }
        /**
         * 是否查询video_info表中的播放数量，弹幕数量，评论数量等信息
         */
        query.setQueryCountInfo(true);

        PaginationResultVO<VideoInfoPost> listByPage = videoInfoPostService.findListByPage(query);

        return getSuccessResponseVO(listByPage);
    }

    @PostMapping("/getVideoCountInfo")
    public ResponseVO getVideoCountInfo() {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();

        VideoInfoPostQuery query = new VideoInfoPostQuery();
        query.setUserId(tokenUserInfoDto.getUserId());
        query.setStatus(VideoStatusEnum.STATUS3.getStatus());
        Integer auditPassCount = videoInfoPostService.findCountByParam(query);

        query.setStatus(VideoStatusEnum.STATUS4.getStatus());
        Integer auditFailCount = videoInfoPostService.findCountByParam(query);

        query.setStatus(null);
        query.setExcludeStatusArray(new Integer[]{VideoStatusEnum.STATUS3.getStatus(), VideoStatusEnum.STATUS4.getStatus()});
        Integer inProgress = videoInfoPostService.findCountByParam(query);

        VideoStatusCountInfoVO rspVO = new VideoStatusCountInfoVO();
        rspVO.setAuditPassCount(auditPassCount);
        rspVO.setAuditFailCount(auditFailCount);
        rspVO.setInProgress(inProgress);

        return getSuccessResponseVO(rspVO);
    }

    /**
     * 点击编辑视频时，获取视频信息
     */
    @PostMapping("/getVideoByVideoId")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO getVideoByVideoId(@Validated @NotBlank String videoId) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        VideoInfoPost videoInfoPost = videoInfoPostService.getVideoInfoPostByVideoId(videoId);
        if (!Objects.equals(tokenUserInfoDto.getUserId(), videoInfoPost.getUserId())) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        VideoInfoFilePostQuery fileQuery = new VideoInfoFilePostQuery();
        fileQuery.setUserId(tokenUserInfoDto.getUserId());
        fileQuery.setVideoId(videoId);
        fileQuery.setOrderBy("file_index asc");
        List<VideoInfoFilePost> filePostList = videoInfoFilePostService.findListByParam(fileQuery);

        VideoPostEditInfoVO rspVO = new VideoPostEditInfoVO();
        rspVO.setVideoInfo(videoInfoPost);
        rspVO.setVideoInfoFileList(filePostList);

        return getSuccessResponseVO(rspVO);
    }

    @PostMapping("/saveVideoInteraction")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO saveVideoInteraction(@Validated @NotBlank String videoId, String interaction) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        videoInfoPostService.changeVideoInteraction(tokenUserInfoDto.getUserId(), videoId, interaction);

        return getSuccessResponseVO(null);
    }

    /**
     * 删除视频
     */
    @PostMapping("/deleteVideo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO deleteVideo(@Validated @NotBlank String videoId) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        videoInfoPostService.deleteVideoInfo(tokenUserInfoDto.getUserId(), videoId);

        return getSuccessResponseVO(null);
    }

    @PostMapping("/delComment")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO delComment(@Validated @NotNull Integer commentId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();

        VideoComment videoComment = videoCommentService.getVideoCommentByCommentId(commentId);
        if (videoComment == null || !Objects.equals(videoComment.getVideoUserId(), tokenUserInfoDto.getUserId())) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        videoCommentService.deleteVideoCommentByCommentIdOrPcommentId(commentId);

        return getSuccessResponseVO(null);
    }

    @PostMapping("/loadDanmu")
    public ResponseVO loadDanmu(@Validated Integer pageNo, Integer pageSize, String videoId) {

        VideoDanmuQuery query = new VideoDanmuQuery();
        query.setPageNo(pageNo);
        /**
         * 视频发布者ID
         */
        query.setVideoUserId(getTokenUserInfoDto().getUserId());
        query.setPageSize(pageSize);
        query.setVideoId(videoId);
        query.setQueryUserInfo(true);
        query.setQueryVideoInfo(true);
        query.setOrderBy("post_time desc");

        return getSuccessResponseVO(videoDanmuService.findListByPage(query));
    }

    @PostMapping("/delDanmu")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO delDanmu(@Validated @NotNull Integer danmuId) {

        VideoDanmu videoDanmu = videoDanmuService.getVideoDanmuByDanmuId(danmuId);
        if (videoDanmu == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        String videoId = videoDanmu.getVideoId();
        VideoInfo videoInfo = videoInfoService.getVideoInfoByVideoId(videoId);
        if (videoInfo == null || !Objects.equals(videoInfo.getUserId(), getTokenUserInfoDto().getUserId())) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        videoDanmuService.deleteVideoDanmuByDanmuId(danmuId);

        return getSuccessResponseVO(null);
    }
}
